using System.Collections.ObjectModel;
using Core;
using Core.Event;
using Core.File;
using Game;

public class PlayerBagModule : PlayerModule {
    public WeaponBag WeaponBag {
        get;
        private set;
    }
    
    public WeaponEffectBagSO WeaponEffectBag {
        get;
        private set;
    }

    public int WeaponEffectBagCapacity {
        get {
            return WeaponEffectBag.EffectIdList.Count;
        }
    }
    
    
    public PlayerBagModule(Player player) : base(player) { }
    public override void Init() {
        base.Init();
        WeaponEffectBag = AssetLoader.LoadAsset<WeaponEffectBagSO>("WeaponEffectBag");
        WeaponBag = FileOperator.ReadFile<WeaponBag>("WeaponBag");
        GameEventMgr.Ins.RegisterEvent(GameEventID.WeaponEdit,onSaveWeaponBag);
        GameEventMgr.Ins.RegisterEvent(GameEventID.WeaponBagEdit,onSaveWeaponBag);
    }

    public override void Release() {
        GameEventMgr.Ins.UnRegisterEvent(GameEventID.WeaponEdit,onSaveWeaponBag);
        GameEventMgr.Ins.RegisterEvent(GameEventID.WeaponBagEdit,onSaveWeaponBag);
    }
    #region 武器背包

    public void AddWeapon(Weapon weapon) {
        WeaponBag.AddWeapon(weapon);
        GameEventMgr.Ins.PushEvent(GameEventID.WeaponBagEdit);
    }

    public void RemoveWeapon(Weapon weapon) {
        if (mPlayer.EquipModule.GetWeapon() == weapon) {
            WeaponBag.RemoveWeapon(weapon);
            mPlayer.EquipModule.UseWeapon(mPlayer.EquipModule.CurIndex + 1);
        }
        GameEventMgr.Ins.PushEvent(GameEventID.WeaponBagEdit);
    }
    
    public ReadOnlyCollection<uint> GetWeaponEffectList() {
        return WeaponEffectBag.EffectIdList.AsReadOnly();
    }

    private void onSaveWeaponBag(GameEvent evt) {
        FileOperator.SaveData(WeaponBag,"WeaponBag");
    }
    #endregion
}
